package com.yonyou.pmclouds.safetypatrol.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.entity.StandardType;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolDetailMapper;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolMapper;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐老板
 * @date 2020/4/15   17:22
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = SafetypatrolQuery.class)
public class SafetypatrolQueryImpl implements SafetypatrolQuery {
    public static Logger logger = LoggerFactory.getLogger(SafetypatrolQueryImpl.class);

    @Resource
    private SafetypatrolMapper safetypatrolMapper;
    @Resource
    private SafetypatrolDetailMapper safetypatrolDetailMapper;

    //分页查询
    @Override
    public Map<String, Object> queryByCondition(String keyword, Integer pageIndex, Integer pageSize, byte stateCode) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        List<SafetypatrolVO> list = null;

        int total = safetypatrolMapper.countPage(keyword, RuntimeEnvironment.getTenantId(), stateCode);
        if (total > 0) {
            list = safetypatrolMapper.queryByPage(keyword, RuntimeEnvironment.getTenantId(), pageIndex, pageSize, stateCode);
        }
            //为所属分类赋值
            if (list!=null){
                for (SafetypatrolVO safetypatrolVO : list) {
                    checkSafetypatrolVOType(safetypatrolVO);
                }

                //设置检查项数量
                for (SafetypatrolVO safetypatrolVO : list) {
                    List<SafetypatrolDetailVO> detailVOS = safetypatrolDetailMapper.findById(safetypatrolVO.getPkSafetypatrol());
                    if (detailVOS!=null){
                        BeanHelper.setProperty(safetypatrolVO,"detailcount" , detailVOS.size());
                    }
                }
            }
        map.put("total", total);
        map.put("result", list);
        return map;
    }






    @Override
    public SafetypatrolVO queryById(String pk_safetypatrol)throws BusinessException {
        Map map =new HashMap();
        map.put("pk_safetypatrol", pk_safetypatrol);
        SafetypatrolVO safetypatrolVO= safetypatrolMapper.findById(map);
        logger.debug("service层得到的实体类为{}",safetypatrolVO);
        return safetypatrolVO;
    }



    public void checkSafetypatrolVOType(SafetypatrolVO safetypatrolVO)throws BusinessException{
        if (safetypatrolVO.getStandardType()==1){
            BeanHelper.setProperty(safetypatrolVO, "standardtypename", StandardType.COMMONCHECK);
        }else if(safetypatrolVO.getStandardType()==2){
            BeanHelper.setProperty(safetypatrolVO, "standardtypename", StandardType.WEEKCHECK);
        }else if(safetypatrolVO.getStandardType()==3){
            BeanHelper.setProperty(safetypatrolVO, "standardtypename", StandardType.MONTHCHECK);
        }else {
            BeanHelper.setProperty(safetypatrolVO, "standardtypename", StandardType.QUARTERCHECK);
        }
    }
}


